//
//  XXGKNet.m
//  上海杨浦
//
//  Created by FrankChen on 13-5-26.
//  Copyright (c) 2013年 FrankChen. All rights reserved.
//

#import "XXGKNet.h"

@implementation XXGKNet


@synthesize linkArray;
@synthesize titleArray;

-(NSString *) getHtml:(NSString *)webUrl{
    
    NSString *retStr =[[[NSString alloc]init]autorelease];
        
    NSURL *url = [NSURL URLWithString:webUrl];
        
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    retStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
    if([retStr isEqual:@""]){
        [self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];
    }
        
    return retStr;
}
    
-(void)showAlert{
    UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alert show];
}


-(NSMutableArray *) getData:(NSString *)html{
    NSString *htmlNew=[[NSString alloc]init];
    htmlNew=[html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *tag1=[[NSString alloc]init];
    tag1=@"width=\"3\" height=\"3\" /></td>";
    NSString *tag2=[[NSString alloc]init];
    tag2=@"</tr>";
    
    NSMutableArray *mutableArray=[[NSMutableArray alloc] init];
    
    Boolean flag=true;
    //for(int index=0;index<15;index++){
    while (flag) {
        @try {
            NSRange range1=[htmlNew rangeOfString:tag1];
            int startIndex1=range1.length+range1.location;
            
            htmlNew=[htmlNew substringFromIndex:startIndex1];
            
            NSRange range2=[htmlNew rangeOfString:tag2];
            
            int endIndex1=range2.location;
            
            NSString * newInfo=[[NSString alloc]init];
            newInfo=[htmlNew substringToIndex:endIndex1];
            [mutableArray addObject:newInfo];
            
            htmlNew=[htmlNew substringFromIndex:range2.location];
        }
        @catch (NSException *exception) {
            flag=false;
        }
        @finally {
            
        }
    }  
    //}
    
    return mutableArray;
}


-(void)handleData:(NSMutableArray *)dataArray :(int)flag{
    NSMutableArray *_linkArray=[[NSMutableArray alloc]init];
    NSMutableArray *_titleArray=[[NSMutableArray alloc]init];
    for(NSString *str in dataArray){
        NSString *temp=[[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] substringFromIndex:10];
        //获取url地址
        NSString *tag0=@"<a href=\"";
        NSRange range0=[temp rangeOfString:tag0];
        NSString *tempLink1=[temp substringFromIndex:(range0.location+range0.length)];
        
        NSString *tag1=@"\"";
        int tag1Location=[tempLink1 rangeOfString:tag1].location;
        tempLink1=[tempLink1 substringToIndex:(tag1Location)];
        
        NSString *weburl=@"";
        switch (flag) {
            case 1:
                weburl=@"http://www.shyp.gov.cn/ypqxxgk/zfxxgk/zfwj/qzfwj/";
                break;
            case 2:
                weburl=@"http://www.shyp.gov.cn/ypqxxgk/zfxxgk/zfwj/qzfbgswj/";
                break;
            case 3:
                weburl=@"http://www.shyp.gov.cn/ypqxxgk/zfxxgk/zfhy/qzfcwhy/";
                break;
            case 4:
                weburl=@"http://www.shyp.gov.cn/ypqxxgk/zfxxgk/zfhy/qzfqthy/";
                break;
            case 5:
                weburl=@"http://www.shyp.gov.cn/ypqxxgk/zfxxgk/zfhy/qzfgzhy/";
                break;
            case 6:
                weburl=@"http://www.shyp.gov.cn/ypqxxgk/zfxxgk/rsrm/";
                break;
            case 7:
                weburl=@"http://www.shyp.gov.cn/ypqxxgk/zfxxgk/qzfzzgknb/";
                break;
        }
        NSString *biaozhi=[tempLink1 substringToIndex:3];
        if([biaozhi isEqual:@"../"]){
            if ([[tempLink1 substringToIndex:4] isEqualToString:@"../."]) {
                weburl=[[weburl substringToIndex:38] stringByAppendingString:[tempLink1 substringFromIndex:6]];
            }
            else{
                weburl=[[weburl substringToIndex:38] stringByAppendingString:[tempLink1 substringFromIndex:3]];
            }
        }
        else{
          weburl=[weburl stringByAppendingString:[tempLink1 substringFromIndex:2]];
        }

        [_linkArray addObject:weburl];
        //获取正文标题
        NSString *tag2=@"\"_blank\">";
        NSString *tag3=@"</a>";
        NSRange range2=[temp rangeOfString:tag2];
        NSString *tempDataContenet=[temp substringFromIndex:(range2.length+range2.location)];
        NSRange range3=[tempDataContenet rangeOfString:tag3];
        tempDataContenet=[[[tempDataContenet substringToIndex:range3.location] stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        
        
        //正文时间

        NSString *tag4=@"class=\"text1\">";
        NSString *tag5=@"</td>";
        NSRange range4=[temp rangeOfString:tag4];
        NSString *tempDataTime=[temp substringFromIndex:(range4.location+range4.length)];
        tempDataTime=[tempDataTime substringToIndex:[tempDataTime rangeOfString:tag5].location];
        tempDataTime=[@" " stringByAppendingString:tempDataTime];
        //组合正文标题＋时间
        NSMutableString *tempData=[[NSMutableString alloc]init];
        [tempData insertString:tempDataTime atIndex:0];
        [tempData insertString:tempDataContenet atIndex:0];
        [_titleArray addObject:tempData];
        
    }
    self.titleArray=_titleArray;
    self.linkArray=_linkArray;
}

@end
